#pragma once
#include<iostream>

namespace key
{
	template<class K>
	struct BSTNode
	{
		K _key;
		BSTNode<K>* _left;
		BSTNode<K>* _right;

		BSTNode(const K& key)
			:_key(key)
			, _left(nullptr)
			, _right(nullptr)
		{
		}
	};

	template<class K>
	class BSTree
	{
	private:
		typedef BSTNode<K> Node;
	public:
		BSTree() = default;

		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}
			Node* cur = _root;
			Node* prev = nullptr;
			while (cur)
			{
				if (key < cur->_key)
				{
					prev = cur;
					cur = cur->_left;
				}
				else if (key > cur->_key)
				{
					prev = cur;
					cur = cur->_right;
				}
				else
					return false;
			}
			cur = new Node(key);
			if (prev->_key < key)
				prev->_right = cur;
			else
				prev->_left = cur;
			return true;
		}

		bool InsertR(const K& key)
		{
			return _InsertR(_root, key);
		}

		bool Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (key < cur->_key)
					cur = cur->_left;
				else if (key > cur->_key)
					cur = cur->_right;
				else
					return true;
			}
			return false;
		}

		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* prev = nullptr;
			while (cur)
			{
				//find the key
				if (key < cur->_key)
				{
					prev = cur;
					cur = cur->_left;
				}
				else if (key > cur->_key)
				{
					prev = cur;
					cur = cur->_right;
				}
				else //where is the key
				{
					if (cur->_left == nullptr) //left null & both null
					{
						if (cur != _root)
						{
							if (cur == prev->_left)
								prev->_left = cur->_right;
							else
								prev->_right = cur->_right;
						}
						else
							_root = cur->_right;
						delete cur;
					}
					else if (cur->_right == nullptr) //right null
					{
						if (cur != _root)
						{
							if (cur == prev->_left)
								prev->_left = cur->_left;
							else
								prev->_right = cur->_left;
						}
						else
							_root = cur->_left;
						delete cur;
					}
					else //not null
					{
						Node* minRightParent = cur;
						Node* minRight = cur->_right; //find the min in right
						while (minRight->_left)
						{
							minRightParent = minRight;
							minRight = minRight->_left;
						}
						std::swap(cur->_key, minRight->_key);
						if (minRight == minRightParent->_left)
							minRightParent->_left = minRight->_right;
						else //when not enter "while"
							minRightParent->_right = minRight->_right;
						delete minRight;
					}
					return true;
				}
			}
			return false;
		}

		void InOrder()
		{
			_InOrder(_root);
			std::cout << std::endl;
		}

	private:
		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;
			_InOrder(root->_left);
			std::cout << root->_key << " ";
			_InOrder(root->_right);
		}

		bool _InsertR(Node*& root, const K& x)
		{
			if (root == nullptr)
			{
				root = new Node(x);
				return true;
			}
			if (root->_key < x)
				return _InsertR(root->_right);
			else if (root->_key > x)
				return _InsertR(root->_left);
			else
				return false;
		}

	private:
		Node* _root = nullptr;
	};
}




namespace key_value
{
	template<class K, class V>
	struct BSTNode
	{
		K _key;
		V _value;
		BSTNode<K, V>* _left;
		BSTNode<K, V>* _right;

		BSTNode(const K& key, const V& value)
			:_key(key)
			, _value(value)
			, _left(nullptr)
			, _right(nullptr)
		{
		}
	};

	template<class K, class V>
	class BSTree
	{
	private:
		typedef BSTNode<K, V> Node;
	public:
		BSTree() = default;

		bool Insert(const K& key, const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key, value);
				return true;
			}
			Node* cur = _root;
			Node* prev = nullptr;
			while (cur)
			{
				if (key < cur->_key)
				{
					prev = cur;
					cur = cur->_left;
				}
				else if (key > cur->_key)
				{
					prev = cur;
					cur = cur->_right;
				}
				else
					return false;
			}
			cur = new Node(key, value);
			if (prev->_key < key)
				prev->_right = cur;
			else
				prev->_left = cur;
			return true;
		}

		Node* Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (key < cur->_key)
					cur = cur->_left;
				else if (key > cur->_key)
					cur = cur->_right;
				else
					return cur;
			}
			return nullptr;
		}

		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* prev = nullptr;
			while (cur)
			{
				//find the key
				if (key < cur->_key)
				{
					prev = cur;
					cur = cur->_left;
				}
				else if (key > cur->_key)
				{
					prev = cur;
					cur = cur->_right;
				}
				else //where is the key
				{
					if (cur->_left == nullptr) //left null & both null
					{
						if (cur != _root)
						{
							if (cur == prev->_left)
								prev->_left = cur->_right;
							else
								prev->_right = cur->_right;
						}
						else
							_root = cur->_right;
						delete cur;
					}
					else if (cur->_right == nullptr) //right null
					{
						if (cur != _root)
						{
							if (cur == prev->_left)
								prev->_left = cur->_left;
							else
								prev->_right = cur->_left;
						}
						else
							_root = cur->_left;
						delete cur;
					}
					else //not null
					{
						Node* minRightParent = cur;
						Node* minRight = cur->_right; //find the min in right
						while (minRight->_left)
						{
							minRightParent = minRight;
							minRight = minRight->_left;
						}
						std::swap(cur->_key, minRight->_key);
						if (minRight == minRightParent->_left)
							minRightParent->_left = minRight->_right;
						else //when not enter "while"
							minRightParent->_right = minRight->_right;
						delete minRight;
					}
					return true;
				}
			}
			return false;
		}

		void InOrder()
		{
			_InOrder(_root);
			std::cout << std::endl;
		}

	private:
		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;
			_InOrder(root->_left);
			std::cout << root->_key << ":" << root->_value << std::endl;
			_InOrder(root->_right);
		}

	private:
		Node* _root = nullptr;
	};
}